package app.foo.planetadnzombiedemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import app.foo.planetadnzombiedemo.global.Config;

/**
 * GameView游戏核心类：继承SurfaceView类，实现回调接口以及线程
 */

public class Gameview extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    //相关成员变量声明
    private Context context;//指活动
    private Canvas canvas;
    private Paint paint;
    private SurfaceHolder surfaceHolder;//界面托管类
    private boolean gameRunFlag;//线程管理标志
    //GameView作为管理者，所有的实体均可向其发送请求并处理
    private static Gameview gameView;

    /**
     * 构造方法：初始化所需资源
     * @param context
     */
    public Gameview(Context context) {
        super(context);
        this.context = context;
        this.paint=new Paint();
        this.surfaceHolder=getHolder();
        this.surfaceHolder.addCallback(this);
        this.gameRunFlag=true;
        gameView = this;
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
//启动游戏核心线程
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }

    @Override
    public void run() {
        while (gameRunFlag) {
            long _startTime = System.currentTimeMillis();
            //同步原语:保证操作的原子性即不可分割
            synchronized (surfaceHolder) {
                try {
                    //锁定并获取画布
                    canvas = surfaceHolder.lockCanvas();
                    draw();
                    logic();
                } catch (Exception e) {
                    Log.e("==MyDebug==","异常错误信息："+e.getMessage() );                } finally {
                    //解锁并提交画布
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            //实现游戏的帧数 40毫秒即25帧
            long _endTime = System.currentTimeMillis();
            long _sleepTime = 40 - (_endTime - _startTime);
            if(_sleepTime>0){
                try {
                    Thread.sleep(_sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }


    }
    /**
     * 获得唯一的管理者（GameView）  单例模式
     * @return
     */
    public static Gameview getInstance(){
        return gameView;
    }

    /**
     * 游戏核心逻辑方法：数据更新
     */
    private void logic() {
    }

    /**
     * 游戏绘制逻辑方法：界面绘制
     */
    private void draw() {
        //        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk),
//                0,0,paint);

        canvas.drawBitmap(Config.gameBK,0,0,paint);
        //绘制卡片
        canvas.drawBitmap(Config.seedBank, Config.seedBankLocationX,0,paint);
        //绘制阳光
        Paint _paint=new Paint();
        _paint.setTypeface(Typeface.DEFAULT_BOLD);
        _paint.setTextSize(38);
        canvas.drawText(Config.sunLight+"",Config.deveWidth*2/7,Config.deveHeight/7.5f,_paint);

    }
}

